package org.wattdepot.simulation.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.wattdepot.client.WattDepotClient;

/**
 * Provides access to a WattDepot client based upon wattdepot.simulation.properties.
 * @author Philip Johnson
 *
 */
public class SimulationClient {
  
  /** The path to the wattdepot.simulation.properties file. */
  private static String filePath = System.getProperty("user.home") + 
  "/.wattdepot/client/wattdepot.simulation.properties";

  /**
   * Reads data from the wattdepot.simulation.properties file and uses that to create and
   * initialize a WattDepotClient for the oahugrid simulations.  Throws a RuntimeException if the
   * file cannot be found or the correct properties cannot be read.
   * @return The WattDepotClient based upon the wattdepot.simulation.properties file.  
   */
  public static WattDepotClient getWattDepotClient() {
    return getClient("oahugrid");
  }
  
  
  /**
   * Reads data from the wattdepot.simulation.properties file and uses that to create and
   * initialize a WattDepotClient for the dorm simulations.  Throws a RuntimeException if the
   * file cannot be found or the correct properties cannot be read.
   * @return The WattDepotClient based upon the wattdepot.simulation.properties file.  
   */
  public static WattDepotClient getWattDepotDormClient() {
    return getClient("dorm");
  }
  
  
  /**
   * Returns a WattDepotClient initialized from the contents of the passed file.
   * @param prefix The property prefix string used to distinguish dorm from oahugrid info.
   * @return The WattDepotClient.
   */
  private static WattDepotClient getClient(String prefix) {
    Properties props = new Properties();
    FileInputStream stream = null;
    try {
      stream = new FileInputStream(filePath); 
      props.load(stream);
    }
    catch (Exception e) {
      System.out.format("Fatal error: %s not found.", filePath);
      throw new RuntimeException(e);
    }
    finally {
      try {
        stream.close();
      }
      catch (IOException e) {
        System.out.println("Could not close stream");
      }
    }
    // The properties file was found. Now get the required properties from it.
    String wattDepotUrl = props.getProperty(prefix + ".wattdepot-server.uri", null);
    String username = props.getProperty(prefix + ".wattdepot-server.username", null);
    String password = props.getProperty(prefix + ".wattdepot-server.password", null);
    
    // Abort if they were not present.
    if ((wattDepotUrl == null) ||
        (username == null) ||
        (password == null)) {
      String msg = "Fatal error: wattdepot.simulation.properties is missing properties.";
      System.out.println(msg);
      throw new RuntimeException(msg);
    }
    // Now try to create the WattDepotClient.
    WattDepotClient client = new WattDepotClient(wattDepotUrl, username, password);
    if (!client.isAuthenticated()) {
      String msg = String.format("Could not authenticate: %s %s %s ", wattDepotUrl, username, 
          password);
      System.out.println(msg);
      throw new RuntimeException(msg);
    }
    
    // It's all good.
    return client;
  }
  
}
